package com.example.payer

import android.text.Editable
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.payer.databinding.ActivityAddPayerBinding
import com.example.payer.etity.PostPayerEntity
import com.example.payer.vm.PayerViewModel
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/payer/AddPayerActivity")
class AddPayerActivity : BaseMvvmActivity<ActivityAddPayerBinding>() {

    private val postPayerViewModel:PayerViewModel by viewModels()
    @Autowired
    @JvmField
    var payerId:Int = 0
    override fun initView() {
        ARouter.getInstance().inject(this)
        //收费项目类型选择器
        mDataBinding.ivMore1.setOnClickListener {
            val list = mutableListOf("物业费","水电费","医药费")
            val optionPicker = OptionPicker(this)
            optionPicker.setData(list)
            optionPicker.setTitle("请选择收费项目")
            optionPicker.setOnOptionPickedListener { position, item ->
                mDataBinding.etPayer.text = Editable.Factory.getInstance().newEditable(list[position])
            }
            optionPicker.show()
        }
        //开始日期
        mDataBinding.ivMore2.setOnClickListener {
            val datePicker = DatePicker(this)
            datePicker.setTitle("请选择开始日期")
            datePicker.wheelLayout.setRange(DateEntity.target(2000,1,1), DateEntity.target(2600,1,1),
                DateEntity.today())
            datePicker.setOnDatePickedListener { year, month, day ->
                mDataBinding.etStartDate.text = Editable.Factory.getInstance().newEditable("$year-$month-$day")
            }
            datePicker.show()
        }
        //结束日期
        mDataBinding.ivMore3.setOnClickListener {
            val datePicker = DatePicker(this)
            datePicker.setTitle("请选择结束日期")
            datePicker.wheelLayout.setRange(DateEntity.target(2000,1,1), DateEntity.target(2600,1,1),
                DateEntity.today())
            datePicker.setOnDatePickedListener { year, month, day ->
                mDataBinding.etEndDate.text = Editable.Factory.getInstance().newEditable("$year-$month-$day")
            }
            datePicker.show()
        }
        mDataBinding.btnCommit.setOnClickListener {
            var postPayer = PostPayerEntity(
                actualAmount = mDataBinding.etMoney.text.toString().toInt(),
                balanceDeduction = 0,
                chargeAmount = mDataBinding.etMoney.text.toString().toInt(),
                chargeType = mDataBinding.etPayer.text.toString(),
                contactNumber = 15053259809,
                discountAmount = 0,
                effectiveDate = mDataBinding.etStartDate.text.toString(),
                expirationDate = mDataBinding.etEndDate.text.toString(),
                id = 0,
                operator = "嘎嘎嘎",
                payerId = payerId,
                payerName = "水豚噜噜",
                paymentMethod = "支付宝",
                paymentStatus = "未缴费",
                paymentTime = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()),
                penalty = 0,
                receivableAmount = mDataBinding.etMoney.text.toString().toInt()
            )
            postPayerViewModel.postPayer(postPayer)
        }
        postPayerViewModel.mPostPayerSuccess.observe(this){
            ToastUtils.showLong("添加成功")
            postPayerViewModel.getPayer(0)
            ARouter.getInstance().build("/payer/PayerActivity").navigation()
        }
    }

    override fun getLayoutId(): Int =R.layout.activity_add_payer
}